<template>
  <div class="login">
    <!-- 登录组件 -->
    <div class="login_box">
      <!-- 头部图片 -->
      <div class="avatar">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 登录表单 -->
      <el-form ref="loginRef" :model="loginForm" class="login_form" :rules="loginRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item style="float:right">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      // 预校验用户名密码
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  },

  methods: {
    resetLogin() {
      // console.log(this);
      this.$refs.loginRef.resetFields();
    },
    login() {
      // validate对表单校验的方法,参数是个回调函数,校验结束后会返回两个参数true和false
      this.$refs.loginRef.validate(async flag => {
        if (!flag) return;
        const res = await this.axios.post("login", this.loginForm);
        // console.log(res);
        if (res.meta.status == 200) {
          this.$mg.success("登陆成功");
          // console.log("登陆成功");
          // console.log(res);
          // 保存token到本地
          sessionStorage.setItem("token", res.data.token);
          // 跳转到首页
          this.$router.push("/home");
        } else {
          this.$mg.error(res.meta.msg);
          // console.log("登陆失败");
        }
      });
    }
  },

  created() {}
}
</script>

<style lang='less' scoped>
.login {
  background-color: #2b4b6b;
  width: 100%;
  height: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      border: 1px solid #eee;
      padding: 10px;
      box-shadow: 0 0 30px green;
      background-color: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: green;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      box-sizing: border-box;
      padding: 20px;
    }
  }
}
</style>
